﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Configuration;

namespace yncTech.Common
{
    public class SendMail
    {
        private static string mailServer = string.Empty, mailUser = string.Empty, mailPassword = string.Empty, mailForm = string.Empty, displayName = string.Empty;

        #region SendMail
        static SendMail()
        {
            mailServer = ConfigurationManager.AppSettings["MailServer"].Trim();
            mailUser = ConfigurationManager.AppSettings["MailUser"].Trim();
            mailForm = ConfigurationManager.AppSettings["MailFrom"].Trim();
            mailPassword = ConfigurationManager.AppSettings["MailPassword"].Trim();
            displayName = ConfigurationManager.AppSettings["DisplayName"].TrimEnd();
        }
        #endregion

        #region Send
        public static void Send(string[] mailTo, string mailSubject, string mailBody)
        {
            try
            {
                SmtpClient mailObject = new SmtpClient(mailServer);
                mailObject.Timeout = 15000;

                mailObject.Credentials = new NetworkCredential(mailUser, mailPassword);
                MailMessage messageObject = new MailMessage();
                messageObject.From = new MailAddress(mailForm, displayName);
                for (int i = 0; i < mailTo.Length; i++)
                {
                    if (mailTo[i] != "")
                    {
                        messageObject.To.Add(mailTo[i]);
                    }

                }
                messageObject.BodyEncoding = Encoding.UTF8;
                messageObject.Subject = mailSubject;
                messageObject.Body = mailBody;
                messageObject.IsBodyHtml = true;
                mailObject.Send(messageObject);
            }
            catch (Exception ex)
            {
                //FileStream fs = new FileStream("c:\\" + Guid.NewGuid().ToString() + ".txt", FileMode.Create);
                //StreamWriter sw = new StreamWriter(fs,Encoding.Default);
                //sw.Write(ex.Message);
                //sw.Flush();
                //sw.Close();
                //fs.Close();
            }
        }

        #endregion

        #region SendHtml
        public static void SendHtml(string[] mailTo, string mailSubject, string mailBody)
        {
            try
            {
                SmtpClient mailObject = new SmtpClient(mailServer);
                mailObject.Timeout = 15000;

                mailObject.Credentials = new NetworkCredential(mailUser, mailPassword);
                MailMessage messageObject = new MailMessage();
                messageObject.From = new MailAddress(mailForm, displayName);
                for (int i = 0; i < mailTo.Length; i++)
                {
                    if (mailTo[i] != "")
                    {
                        messageObject.To.Add(mailTo[i]);
                    }

                }
                messageObject.BodyEncoding = Encoding.UTF8;
                messageObject.Subject = mailSubject;
                messageObject.Body = mailBody;
                messageObject.IsBodyHtml = true;
                mailObject.Send(messageObject);
            }
            catch (Exception ex)
            {
                //FileStream fs = new FileStream("c:\\" + Guid.NewGuid().ToString() + ".txt", FileMode.Create);
                //StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                //sw.Write(ex.Message);
                //sw.Flush();
                //sw.Close();
                //fs.Close();
            }
        }
        public static void SendHtml(string[] mailTo,string[] mailCC, string mailSubject, string mailBody)
        {
            try
            {
                SmtpClient mailObject = new SmtpClient(mailServer);
                mailObject.Timeout = 15000;
                mailObject.Credentials = new NetworkCredential(mailUser, mailPassword);
                MailMessage messageObject = new MailMessage();
                messageObject.From = new MailAddress(mailForm, displayName);
                for (int i = 0; i < mailTo.Length; i++)
                {
                    if (mailTo[i] != "")
                    {
                        messageObject.To.Add(mailTo[i]);
                    }

                }
                if (mailCC.Length > 0)
                {
                    for (int i = 0; i < mailCC.Length; i++)
                    {
                        if (mailCC[i] != "")
                        {
                            messageObject.CC.Add(mailCC[i]);
                        }
                    }
                }

                messageObject.BodyEncoding = Encoding.UTF8;
                messageObject.Subject = mailSubject;
                messageObject.Body = mailBody;
                messageObject.IsBodyHtml = true;
                mailObject.Send(messageObject);
            }
            catch (Exception ex)
            {
                //FileStream fs = new FileStream("c:\\" + Guid.NewGuid().ToString() + ".txt", FileMode.Create);
                //StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                //sw.Write(ex.Message);
                //sw.Flush();
                //sw.Close();
                //fs.Close();
            }
        }
        #endregion

        #region Send With Attachment
        public static void Send(string[] mailTo, string mailSubject, string mailBody, string filePath, string mailFromAddress)
        {
            try
            {
                SmtpClient mailObject = new SmtpClient(mailServer);

                mailObject.Credentials = new NetworkCredential(mailUser, mailPassword);
                MailMessage messageObject = new MailMessage();
                messageObject.From = new MailAddress(mailFromAddress);
                for (int i = 0; i < mailTo.Length; i++)
                {
                    if (mailTo[i] != "")
                    {
                        messageObject.To.Add(mailTo[i]);
                    }
                }
                Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet);
                messageObject.Attachments.Add(data);
                messageObject.BodyEncoding = Encoding.UTF8;
                messageObject.Subject = mailSubject;
                messageObject.Body = mailBody;
                messageObject.IsBodyHtml = true;
                mailObject.Send(messageObject);
            }
            catch (Exception ex)
            {
                //FileStream fs = new FileStream("c:\\" + Guid.NewGuid().ToString() + ".txt", FileMode.Create);
                //StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                //sw.Write(ex.Message);
                //sw.Flush();
                //sw.Close();
                //fs.Close();
            }
        }
        #endregion

        #region Send With Attachment
        public static void Send(string[] mailTo, string mailSubject, string mailBody, string filePath, string mailFromAddress, MailPriority priority)
        {
            try
            {
                SmtpClient mailObject = new SmtpClient(mailServer);

                mailObject.Credentials = new NetworkCredential(mailUser, mailPassword);
                MailMessage messageObject = new MailMessage();
                messageObject.From = new MailAddress(mailFromAddress);
                for (int i = 0; i < mailTo.Length; i++)
                {
                    if (mailTo[i] != "")
                    {
                        messageObject.To.Add(mailTo[i]);
                    }
                }
                Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet);
                messageObject.Attachments.Add(data);
                messageObject.BodyEncoding = Encoding.UTF8;
                messageObject.Subject = mailSubject;
                messageObject.Body = mailBody;
                messageObject.IsBodyHtml = true;
                messageObject.Priority = MailPriority.High;
                mailObject.Send(messageObject);
            }
            catch (Exception ex)
            {
                //FileStream fs = new FileStream("c:\\" + Guid.NewGuid().ToString() + ".txt", FileMode.Create);
                //StreamWriter sw = new StreamWriter(fs,Encoding.Default);
                //sw.Write(ex.Message);
                //sw.Flush();
                //sw.Close();
                //fs.Close();
            }
        }
        #endregion
    }
}
